<template>
    <div class='fxn-body'>
        <div class="fxn-wrap">
            <div class="fxn-wrap-left">
                <div class="info"><i class="iconfont icon-tasklist"></i>字典类型</div>
                <div class="rows">
                    <div class="item" v-for="(item,index) in dataType" :key="index" :class="[type == item.value ? 'active' : '']" @click="handlePath(item.value)">
                        <div class="content">{{item.label}}</div>
                    </div>
                </div>
            </div>
            <!-- Left End -->
            <div class="fxn-wrap-right">
                <div class="fxn-top">
                    <el-button type="danger" icon="Plus" @click="handleOp(1)">添加</el-button>
                </div>
                <!-- Top End -->
                <div class="fxn-section wrap-fixed fxn-margin-t-20">
                    <el-table v-loading="loading" border :data="listData">
                        <el-table-column label="ID" prop="id" width="80" />
                        <el-table-column label="字段名称" prop="value" />
                        <el-table-column label="字典标识" prop="name" />
                        <el-table-column label="状态" prop="status" width="80">
                            <template #default="scope">
                                <el-switch v-model="scope.row.status" :active-value="1" :inactive-value="0" active-color="#13ce66" inactive-color="#ff4949" @click="handleStatus(scope.row)"></el-switch>
                            </template>
                        </el-table-column>
                        <el-table-column label="操作" width="120">
                            <template #default="scope">
                                <el-link type="danger" :underline="false" @click="handleOp(2,scope.row)">编辑</el-link>
                                <!-- <el-link type="danger" :underline="false" @click="handleDel(scope.row)">删除</el-link> -->
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
                <!-- Section End -->
                <div class="fxn-page wrap-fixed">
                    <el-pagination v-model:current-page="page" v-model:page-size="perPage" :page-sizes="[10, 50, 100, 200, 500]" layout="total, sizes, prev, pager, next" :total="count" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
                </div>
            </div>
            <!-- Right End -->
        </div>
        <!-- Wrap End -->

        <!-- 操作菜单 -->
        <el-dialog v-model="opDialog" :title="opTitle" width="700" :before-close="handleOpClose">
            <el-form :model="opForm" ref="opRef" :rules="opRules" label-width="100">
                <el-form-item label="字典名称" prop="value">
                    <el-input v-model="opForm.value" placeholder="请输入字典名称" @input="handleTo"></el-input>
                </el-form-item>
                <el-form-item label="字典标识" prop="name">
                    <el-input v-model="opForm.name" placeholder="请输入字典标识" :disabled="opForm.id ? true : false"></el-input>
                </el-form-item>
            </el-form>
            <template #footer>
                <el-button @click="handleOpClose">取消</el-button>
                <el-button type="danger" :loading="btnLoading" @click="handleSubmit">确认</el-button>
            </template>
        </el-dialog>
        <!-- 操作菜单 End -->
    </div>
</template>

<script>
    import { defineComponent, reactive, getCurrentInstance, onMounted, toRefs, computed } from 'vue'
    import { useRouter } from 'vue-router'
    import { useStore } from 'vuex'
    import { pinyin } from 'pinyin-pro';
    export default defineComponent({
        name: '',
        setup() {
            const { proxy } = getCurrentInstance();
            const router = useRouter();
            const store = useStore();

            //初始化数据
            const state = reactive({
                type: 'follow_stage',
                count: 0,
                page: 0,
                perPage: 10,
                loading: false,
                listData: [],
                dataType: computed(() => store.state.dataType),

                //操作
                btnLoading: false,
                opDialog: false,
                opRef: '',
                opTitle: '',
                opForm: {
                    name: '',
                    value: '',
                },
                opRules: {
                    value: [
                        { required: true, message: '请输入字典名称', trigger: 'blur' },
                        { validator: function(rule,value,callback){
                            if (/[\u4e00-\u9fff]+|[!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~]+/g.test(value) == false) {
                                callback(new Error("请输入中文"));
                            } else {
                                //校验通过
                                callback();
                            }
                        },trigger: 'blur'}
                    ],
                    name: [
                        { required: true, message: '请输入字典标识', trigger: 'blur' },
                        { validator: function(rule,value,callback){
                            if (/[a-zA-z]$/.test(value) == false) {
                                callback(new Error("请输入英文"));
                            } else {
                                //校验通过
                                callback();
                            }
                        },trigger: 'blur'}
                    ],
                },
            })

            //获取数据
            const getList = (page,perPage) => {
                state.loading = true;
                proxy.$http.get('/sys/Config/index',{
                    page: page ? page : 1,
                    perPage: perPage ? perPage : 10,
                    type: state.type,
                })
                .then(res=> {
                    state.loading = false;
                    const { code, msg ,data } = res;
                    if(code != 1){
                        return proxy.$msg.error(msg);
                    }else{
                        state.count = data.count;
                        state.page = data.page;
                        state.perPage = data.perPage;
                        state.listData = data.list;
                    }
                })
            }

            //汉字转拼音
            const handleTo = (e) => {
                let name = pinyin(e, { toneType: 'none' });
                if(!state.opForm.id){
                    state.opForm.name = name.replace(/\s+/g,'');
                }
            }

            //操作
            const handleOp = (type,row) => {
                if(type == 1){
                    state.opForm = {};
                    state.opForm.type = state.type;
                    state.opTitle = '添加字典';
                }else{
                    state.opTitle = '编辑字典';
                    proxy.$http.get('/sys/Config/op', {id: row.id})
                    .then(res=> {
                        const { code, msg ,data } = res;
                        if(code != 1){
                            return proxy.$msg.error(msg);
                        }else{
                            state.opForm = data;
                        }
                    })
                }
                state.opDialog = true;
            }

            //提交操作
            const handleSubmit = () => {
                state.opRef.validate((valid) => {
                    if(valid){
                        proxy.$http.post('/sys/Config/op',state.opForm)
                        .then(res=> {
                            const { code, msg ,data } = res;
                            if(code != 1){
                                return proxy.$msg.error(msg);
                            }else{
                                state.opDialog = false;
                                proxy.$refs.opRef.resetFields();
                                proxy.$msg.success('提交成功!');
                                getList(state.page,state.perPage);
                            }
                        })
                    }
                })
            }

            //关闭菜单操作
            const handleOpClose = () =>{
                state.opDialog = false;
                proxy.$refs.opRef.resetFields();
            }

            //修改状态
            const handleStatus = (row) => {
                proxy.$http.post('/sys/Config/state',{id: row.id, status:row.status})
                .then(res=>{
                    const { code, msg , data } = res;
                    if(code != 1){
                        return proxy.$msg.error(msg);
                    }else{
                        proxy.$msg.success('修改成功!');
                        getList(state.page,state.perPage);
                    }
                })
            }

            //删除
            const handleDel = (row) => {
                proxy.$confirm("此操作将会删除该数据, 是否继续?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning",
                })
                .then(async () => {
                    proxy.$http.post('/sys/Config/remove',{id: row.id})
                    .then(res=> {
                        let { code, msg, data } = res;
                        if(code != 1){
                            return proxy.$msg.error(msg);
                        }else{
                            proxy.$msg.success('删除成功!');
                            getList(state.page,state.perPage);
                        }
                    })
                })
                .catch(() => {})
            }

            //跳转
            const handlePath = (e) => {
                state.type = e;
                getList(1,10);
            }

            //分页
            const handleSizeChange = (val) => {
                state.perPage = val;
                getList(state.page,state.perPage);
            }

            //页码
            const handleCurrentChange = (val) => {
                state.page = val;
                getList(state.page,state.perPage);
            }
            
            //Dom加载
            onMounted(()=>{
                getList();
            })
            
            return {
                ...toRefs(state),
                
                //请求方法
                getList,
                handleTo,
                handleOp,
                handleSubmit,
                handleOpClose,
                handleStatus,
                handleDel,
                
                //组件方法
                handlePath,
                handleSizeChange,
                handleCurrentChange,
            }

        },
    })
</script>

<style scoped>
    
</style>